#!/bin/bash
name=virtio-net-controller
bd=$(dirname $0)
wdir=$(dirname $bd)
cd $wdir

set -e
# create dist tarball in current directory
#VER=$(awk -F'[\[\]]' '/AC_INIT/ {print $4}' configure.ac)
VER=$($bd/get_ver.sh)
TARBALL="$name-$VER.tar.gz"
if [ ! -e $TARBALL ] ; then
  # controller
  ./autogen.sh
  ./configure
  # source dist
  make dist
  tar -xvzf $name-$VER.tar.gz
  rm -f $name-$VER.tar.gz
  tar -cvzf $name-$VER.tar.gz $name-$VER
  rm -rf $name-$VER
fi

TOPDIR=$(rpmbuild -E "%_topdir")
if [ ! -n ${TOPDIR} ]; then
  mkdir -p ${TOPDIR}/{SOURCES,RPMS,SRPMS,SPECS,BUILD,BUILDROOT}
else
  mkdir -p $HOME/rpmbuild/{SOURCES,RPMS,SRPMS,SPECS,BUILD,BUILDROOT}
fi

if [ -z "$ghprbPullId" ]; then
    _rev="${BUILD_NUMBER:-1}"
else
    _rev="pr$ghprbPullId"
fi


# find . -name "*.patch" -exec cp {} $HOME/rpmbuild/SOURCES \;

# BUILD_NUMBER is an env var passed by Jenkins
# https://stackoverflow.com/questions/16155792/using-jenkins-build-number-in-rpm-spec-file
# fakeroot 
# rpmbuild -bb $SPECfile
echo "start rpm build..."
rpmbuild -ta $name-$VER.tar.gz

# VER=$VER REV=${_rev} ./build_scripts/upload_rpm.sh
